#include "vmalloc.h"

#ifdef __linux__
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#endif

static void *(*vMalloc_g)(size_t sz) = malloc;
static void (*vFree_g)(void *ptr) = free;
static void *(*vCalloc_g)(size_t nmemb,size_t sz) = calloc;
static void *(*vRealloc_g)(void *ptr,size_t sz) = realloc;


void vMallocInitHooks(mallocHooks_t *hooks)
{
    if(hooks)
    {
        vMalloc_g = (hooks->malloc_fn) ? hooks->malloc_fn : malloc;
        vFree_g = (hooks->free_fn) ? hooks->free_fn :free;
        vCalloc_g = (hooks->calloc_fn) ? hooks->calloc_fn : calloc;
        vRealloc_g = (hooks->realloc_fn) ? hooks->realloc_fn : realloc;
    }
}

void *vMalloc(size_t sz)
{
    void *ptr = vMalloc_g(sz);
    assert(ptr);
    return ptr;
}

void vFree(void *ptr)
{
    vFree_g(ptr);
}

void *vCalloc(size_t nmemb, size_t sz)
{
    void *ptr = vCalloc_g(nmemb,sz);
    assert(ptr);
    return ptr;
}

void *vRealloc(void *ptr, size_t sz)
{
    void *ret = vRealloc_g(ptr,sz);
    assert(ret);
    return ret;
}

char *vStrdup(const char *str)
{
    char *ptr = strdup(str);
    assert(ptr);
    return ptr;
}
